// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// ███╗   ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗  ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║   ██║   ██║   ██║██║     █████╗
// ██║╚██╗██║██║   ██║   ██║   ██║██║     ██╔══╝
// ██║ ╚████║╚██████╔╝   ██║   ██║╚██████╗███████╗
// ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

#nullable restore

using Elastic.Clients.Elasticsearch.Serverless.Fluent;
using Elastic.Clients.Elasticsearch.Serverless.Serialization;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.Serverless.QueryDsl;

[JsonConverter(typeof(FunctionScoreConverter))]
public sealed partial class FunctionScore
{
	internal FunctionScore(string variantName, object variant)
	{
		if (variantName is null)
			throw new ArgumentNullException(nameof(variantName));
		if (variant is null)
			throw new ArgumentNullException(nameof(variant));
		if (string.IsNullOrWhiteSpace(variantName))
			throw new ArgumentException("Variant name must not be empty or whitespace.");
		VariantName = variantName;
		Variant = variant;
	}

	internal object Variant { get; }
	internal string VariantName { get; }

	public static FunctionScore Exp(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.UntypedDecayFunction decayFunction) => new FunctionScore("exp", decayFunction);
	public static FunctionScore Exp(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.DateDecayFunction decayFunction) => new FunctionScore("exp", decayFunction);
	public static FunctionScore Exp(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.NumericDecayFunction decayFunction) => new FunctionScore("exp", decayFunction);
	public static FunctionScore Exp(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.GeoDecayFunction decayFunction) => new FunctionScore("exp", decayFunction);
	public static FunctionScore FieldValueFactor(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.FieldValueFactorScoreFunction fieldValueFactorScoreFunction) => new FunctionScore("field_value_factor", fieldValueFactorScoreFunction);
	public static FunctionScore Gauss(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.UntypedDecayFunction decayFunction) => new FunctionScore("gauss", decayFunction);
	public static FunctionScore Gauss(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.DateDecayFunction decayFunction) => new FunctionScore("gauss", decayFunction);
	public static FunctionScore Gauss(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.NumericDecayFunction decayFunction) => new FunctionScore("gauss", decayFunction);
	public static FunctionScore Gauss(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.GeoDecayFunction decayFunction) => new FunctionScore("gauss", decayFunction);
	public static FunctionScore Linear(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.UntypedDecayFunction decayFunction) => new FunctionScore("linear", decayFunction);
	public static FunctionScore Linear(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.DateDecayFunction decayFunction) => new FunctionScore("linear", decayFunction);
	public static FunctionScore Linear(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.NumericDecayFunction decayFunction) => new FunctionScore("linear", decayFunction);
	public static FunctionScore Linear(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.GeoDecayFunction decayFunction) => new FunctionScore("linear", decayFunction);
	public static FunctionScore RandomScore(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.RandomScoreFunction randomScoreFunction) => new FunctionScore("random_score", randomScoreFunction);
	public static FunctionScore ScriptScore(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.ScriptScoreFunction scriptScoreFunction) => new FunctionScore("script_score", scriptScoreFunction);

	[JsonInclude, JsonPropertyName("filter")]
	public Elastic.Clients.Elasticsearch.Serverless.QueryDsl.Query? Filter { get; set; }
	[JsonInclude, JsonPropertyName("weight")]
	public double? Weight { get; set; }

	public bool TryGet<T>([NotNullWhen(true)] out T? result) where T : class
	{
		result = default;
		if (Variant is T variant)
		{
			result = variant;
			return true;
		}

		return false;
	}
}

internal sealed partial class FunctionScoreConverter : JsonConverter<FunctionScore>
{
	public override FunctionScore Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
	{
		if (reader.TokenType != JsonTokenType.StartObject)
		{
			throw new JsonException("Expected start token.");
		}

		object? variantValue = default;
		string? variantNameValue = default;
		Elastic.Clients.Elasticsearch.Serverless.QueryDsl.Query? filterValue = default;
		double? weightValue = default;
		while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
		{
			if (reader.TokenType != JsonTokenType.PropertyName)
			{
				throw new JsonException("Expected a property name token.");
			}

			if (reader.TokenType != JsonTokenType.PropertyName)
			{
				throw new JsonException("Expected a property name token representing the name of an Elasticsearch field.");
			}

			var propertyName = reader.GetString();
			reader.Read();
			if (propertyName == "filter")
			{
				filterValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.Query?>(ref reader, options);
				continue;
			}

			if (propertyName == "weight")
			{
				weightValue = JsonSerializer.Deserialize<double?>(ref reader, options);
				continue;
			}

			if (propertyName == "exp")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.UntypedDecayFunction>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			if (propertyName == "field_value_factor")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.FieldValueFactorScoreFunction?>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			if (propertyName == "gauss")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.UntypedDecayFunction>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			if (propertyName == "linear")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.UntypedDecayFunction>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			if (propertyName == "random_score")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.RandomScoreFunction?>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			if (propertyName == "script_score")
			{
				variantValue = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.ScriptScoreFunction?>(ref reader, options);
				variantNameValue = propertyName;
				continue;
			}

			throw new JsonException($"Unknown property name '{propertyName}' received while deserializing the 'FunctionScore' from the response.");
		}

		var result = new FunctionScore(variantNameValue, variantValue);
		result.Filter = filterValue;
		result.Weight = weightValue;
		return result;
	}

	public override void Write(Utf8JsonWriter writer, FunctionScore value, JsonSerializerOptions options)
	{
		writer.WriteStartObject();
		if (value.Filter is not null)
		{
			writer.WritePropertyName("filter");
			JsonSerializer.Serialize(writer, value.Filter, options);
		}

		if (value.Weight.HasValue)
		{
			writer.WritePropertyName("weight");
			writer.WriteNumberValue(value.Weight.Value);
		}

		if (value.VariantName is not null && value.Variant is not null)
		{
			writer.WritePropertyName(value.VariantName);
			switch (value.VariantName)
			{
				case "exp":
					JsonSerializer.Serialize(writer, value.Variant, value.Variant.GetType(), options);
					break;
				case "field_value_factor":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.FieldValueFactorScoreFunction>(writer, (Elastic.Clients.Elasticsearch.Serverless.QueryDsl.FieldValueFactorScoreFunction)value.Variant, options);
					break;
				case "gauss":
					JsonSerializer.Serialize(writer, value.Variant, value.Variant.GetType(), options);
					break;
				case "linear":
					JsonSerializer.Serialize(writer, value.Variant, value.Variant.GetType(), options);
					break;
				case "random_score":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.RandomScoreFunction>(writer, (Elastic.Clients.Elasticsearch.Serverless.QueryDsl.RandomScoreFunction)value.Variant, options);
					break;
				case "script_score":
					JsonSerializer.Serialize<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.ScriptScoreFunction>(writer, (Elastic.Clients.Elasticsearch.Serverless.QueryDsl.ScriptScoreFunction)value.Variant, options);
					break;
			}
		}

		writer.WriteEndObject();
	}
}

public sealed partial class FunctionScoreDescriptor<TDocument> : SerializableDescriptor<FunctionScoreDescriptor<TDocument>>
{
	internal FunctionScoreDescriptor(Action<FunctionScoreDescriptor<TDocument>> configure) => configure.Invoke(this);

	public FunctionScoreDescriptor() : base()
	{
	}

	private bool ContainsVariant { get; set; }
	private string ContainedVariantName { get; set; }
	private object Variant { get; set; }
	private Descriptor Descriptor { get; set; }

	private FunctionScoreDescriptor<TDocument> Set<T>(Action<T> descriptorAction, string variantName) where T : Descriptor
	{
		ContainedVariantName = variantName;
		ContainsVariant = true;
		var descriptor = (T)Activator.CreateInstance(typeof(T), true);
		descriptorAction?.Invoke(descriptor);
		Descriptor = descriptor;
		return Self;
	}

	private FunctionScoreDescriptor<TDocument> Set(object variant, string variantName)
	{
		Variant = variant;
		ContainedVariantName = variantName;
		ContainsVariant = true;
		return Self;
	}

	private Elastic.Clients.Elasticsearch.Serverless.QueryDsl.Query? FilterValue { get; set; }
	private double? WeightValue { get; set; }

	public FunctionScoreDescriptor<TDocument> Filter(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.Query? filter)
	{
		FilterValue = filter;
		return Self;
	}

	public FunctionScoreDescriptor<TDocument> Weight(double? weight)
	{
		WeightValue = weight;
		return Self;
	}

	public FunctionScoreDescriptor<TDocument> Exp(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.UntypedDecayFunction decayFunction) => Set(decayFunction, "exp");
	public FunctionScoreDescriptor<TDocument> Exp(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.DateDecayFunction decayFunction) => Set(decayFunction, "exp");
	public FunctionScoreDescriptor<TDocument> Exp(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.NumericDecayFunction decayFunction) => Set(decayFunction, "exp");
	public FunctionScoreDescriptor<TDocument> Exp(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.GeoDecayFunction decayFunction) => Set(decayFunction, "exp");
	public FunctionScoreDescriptor<TDocument> FieldValueFactor(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.FieldValueFactorScoreFunction fieldValueFactorScoreFunction) => Set(fieldValueFactorScoreFunction, "field_value_factor");
	public FunctionScoreDescriptor<TDocument> FieldValueFactor(Action<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.FieldValueFactorScoreFunctionDescriptor<TDocument>> configure) => Set(configure, "field_value_factor");
	public FunctionScoreDescriptor<TDocument> Gauss(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.UntypedDecayFunction decayFunction) => Set(decayFunction, "gauss");
	public FunctionScoreDescriptor<TDocument> Gauss(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.DateDecayFunction decayFunction) => Set(decayFunction, "gauss");
	public FunctionScoreDescriptor<TDocument> Gauss(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.NumericDecayFunction decayFunction) => Set(decayFunction, "gauss");
	public FunctionScoreDescriptor<TDocument> Gauss(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.GeoDecayFunction decayFunction) => Set(decayFunction, "gauss");
	public FunctionScoreDescriptor<TDocument> Linear(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.UntypedDecayFunction decayFunction) => Set(decayFunction, "linear");
	public FunctionScoreDescriptor<TDocument> Linear(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.DateDecayFunction decayFunction) => Set(decayFunction, "linear");
	public FunctionScoreDescriptor<TDocument> Linear(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.NumericDecayFunction decayFunction) => Set(decayFunction, "linear");
	public FunctionScoreDescriptor<TDocument> Linear(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.GeoDecayFunction decayFunction) => Set(decayFunction, "linear");
	public FunctionScoreDescriptor<TDocument> RandomScore(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.RandomScoreFunction randomScoreFunction) => Set(randomScoreFunction, "random_score");
	public FunctionScoreDescriptor<TDocument> RandomScore(Action<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.RandomScoreFunctionDescriptor<TDocument>> configure) => Set(configure, "random_score");
	public FunctionScoreDescriptor<TDocument> ScriptScore(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.ScriptScoreFunction scriptScoreFunction) => Set(scriptScoreFunction, "script_score");
	public FunctionScoreDescriptor<TDocument> ScriptScore(Action<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.ScriptScoreFunctionDescriptor> configure) => Set(configure, "script_score");

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		if (FilterValue is not null)
		{
			writer.WritePropertyName("filter");
			JsonSerializer.Serialize(writer, FilterValue, options);
		}

		if (WeightValue.HasValue)
		{
			writer.WritePropertyName("weight");
			writer.WriteNumberValue(WeightValue.Value);
		}

		if (!string.IsNullOrEmpty(ContainedVariantName))
		{
			writer.WritePropertyName(ContainedVariantName);
			if (Variant is not null)
			{
				JsonSerializer.Serialize(writer, Variant, Variant.GetType(), options);
				writer.WriteEndObject();
				return;
			}

			JsonSerializer.Serialize(writer, Descriptor, Descriptor.GetType(), options);
		}

		writer.WriteEndObject();
	}
}

public sealed partial class FunctionScoreDescriptor : SerializableDescriptor<FunctionScoreDescriptor>
{
	internal FunctionScoreDescriptor(Action<FunctionScoreDescriptor> configure) => configure.Invoke(this);

	public FunctionScoreDescriptor() : base()
	{
	}

	private bool ContainsVariant { get; set; }
	private string ContainedVariantName { get; set; }
	private object Variant { get; set; }
	private Descriptor Descriptor { get; set; }

	private FunctionScoreDescriptor Set<T>(Action<T> descriptorAction, string variantName) where T : Descriptor
	{
		ContainedVariantName = variantName;
		ContainsVariant = true;
		var descriptor = (T)Activator.CreateInstance(typeof(T), true);
		descriptorAction?.Invoke(descriptor);
		Descriptor = descriptor;
		return Self;
	}

	private FunctionScoreDescriptor Set(object variant, string variantName)
	{
		Variant = variant;
		ContainedVariantName = variantName;
		ContainsVariant = true;
		return Self;
	}

	private Elastic.Clients.Elasticsearch.Serverless.QueryDsl.Query? FilterValue { get; set; }
	private double? WeightValue { get; set; }

	public FunctionScoreDescriptor Filter(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.Query? filter)
	{
		FilterValue = filter;
		return Self;
	}

	public FunctionScoreDescriptor Weight(double? weight)
	{
		WeightValue = weight;
		return Self;
	}

	public FunctionScoreDescriptor Exp(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.UntypedDecayFunction decayFunction) => Set(decayFunction, "exp");
	public FunctionScoreDescriptor Exp(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.DateDecayFunction decayFunction) => Set(decayFunction, "exp");
	public FunctionScoreDescriptor Exp(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.NumericDecayFunction decayFunction) => Set(decayFunction, "exp");
	public FunctionScoreDescriptor Exp(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.GeoDecayFunction decayFunction) => Set(decayFunction, "exp");
	public FunctionScoreDescriptor FieldValueFactor(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.FieldValueFactorScoreFunction fieldValueFactorScoreFunction) => Set(fieldValueFactorScoreFunction, "field_value_factor");
	public FunctionScoreDescriptor FieldValueFactor<TDocument>(Action<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.FieldValueFactorScoreFunctionDescriptor> configure) => Set(configure, "field_value_factor");
	public FunctionScoreDescriptor Gauss(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.UntypedDecayFunction decayFunction) => Set(decayFunction, "gauss");
	public FunctionScoreDescriptor Gauss(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.DateDecayFunction decayFunction) => Set(decayFunction, "gauss");
	public FunctionScoreDescriptor Gauss(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.NumericDecayFunction decayFunction) => Set(decayFunction, "gauss");
	public FunctionScoreDescriptor Gauss(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.GeoDecayFunction decayFunction) => Set(decayFunction, "gauss");
	public FunctionScoreDescriptor Linear(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.UntypedDecayFunction decayFunction) => Set(decayFunction, "linear");
	public FunctionScoreDescriptor Linear(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.DateDecayFunction decayFunction) => Set(decayFunction, "linear");
	public FunctionScoreDescriptor Linear(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.NumericDecayFunction decayFunction) => Set(decayFunction, "linear");
	public FunctionScoreDescriptor Linear(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.GeoDecayFunction decayFunction) => Set(decayFunction, "linear");
	public FunctionScoreDescriptor RandomScore(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.RandomScoreFunction randomScoreFunction) => Set(randomScoreFunction, "random_score");
	public FunctionScoreDescriptor RandomScore<TDocument>(Action<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.RandomScoreFunctionDescriptor> configure) => Set(configure, "random_score");
	public FunctionScoreDescriptor ScriptScore(Elastic.Clients.Elasticsearch.Serverless.QueryDsl.ScriptScoreFunction scriptScoreFunction) => Set(scriptScoreFunction, "script_score");
	public FunctionScoreDescriptor ScriptScore(Action<Elastic.Clients.Elasticsearch.Serverless.QueryDsl.ScriptScoreFunctionDescriptor> configure) => Set(configure, "script_score");

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		if (FilterValue is not null)
		{
			writer.WritePropertyName("filter");
			JsonSerializer.Serialize(writer, FilterValue, options);
		}

		if (WeightValue.HasValue)
		{
			writer.WritePropertyName("weight");
			writer.WriteNumberValue(WeightValue.Value);
		}

		if (!string.IsNullOrEmpty(ContainedVariantName))
		{
			writer.WritePropertyName(ContainedVariantName);
			if (Variant is not null)
			{
				JsonSerializer.Serialize(writer, Variant, Variant.GetType(), options);
				writer.WriteEndObject();
				return;
			}

			JsonSerializer.Serialize(writer, Descriptor, Descriptor.GetType(), options);
		}

		writer.WriteEndObject();
	}
}